package com.tidb.example.demo.controler;

import com.tidb.example.demo.entity.User;
import com.tidb.example.demo.service.UserService;
import com.tidb.example.demo.util.ThreadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : ${author}
 * @version V1.0
 * @Package ${packageName}
 * @Description: todo
 * @date ${date} ${time}
 */
@RestController
@RequestMapping("/user")
public class UserControler {

      @Autowired
      private  UserService userService ;

      @RequestMapping("/selectByUname")
      @ResponseBody
      public User selectByUname ( String name ) {
            System.out.println("name : " + name) ;
            User user = userService.selectByUname( name ) ;
            System.out.println("username: "+ user.getUname());
            return user ;
      }

      @RequestMapping("/selectAll")
      @ResponseBody
      public List<User> selectAll () {
            return userService.selectAll() ;
      }

      @RequestMapping("/insertOne")
      @ResponseBody
      public String insertOne ( ) {
            User user = new User() ;
            user.setUname("小美") ;
            user.setGender("女") ;
            user.setBirthday(new Date()) ;
            int result = userService.insertOne( user ) ;
            if (result == 0 ) {
                  return "插入失败" ;
            }
            return "插入成功" ;
      }

      @RequestMapping("/update")
      @ResponseBody
      public String udpateById( int id ) {
            System.out.println("id : " + id);
            User user = new User() ;
            user.setUname("老狄") ;
            user.setGender("男") ;
            user.setBirthday(new Date()) ;
            int result = userService.updateById( id, user ) ;
            if ( result == 0 ) {
                  return "更新失败" ;
            }
            return "更新成功" ;
      }

      @RequestMapping("/deleteById")
      @ResponseBody
      public String deleteById ( int id ) {
            System.out.println("id : " + id ) ;
            int result = userService.deleteById( id ) ;
            if (result == 0 ) {
                  return "删除失败" ;
            }
            return "删除成功" ;
      }

      @RequestMapping("/batchSave")
      @ResponseBody
      public String batchSave(int number) {
            List<User> users = new ArrayList<>() ;
            int result = userService.batchSave(users, number) ;
            if (result == 0 ) {
                  return "批量插入失败" ;
            }
            return "批量插入成功" ;
      }
      @RequestMapping("/batchSave2")
      @ResponseBody
      public String batchSave2(int number) throws InterruptedException {

            List<User> users = new ArrayList<>() ;
            int runsize = number/10000 ;

            int result = userService.batchSave2(users, number) ;
            if (result == 0 ) {
                  return "批量插入失败" ;
            }
            return "批量插入成功" ;
      }

      @RequestMapping("/batchInsert")
      @ResponseBody
      public String batchInsert(int number) {
            List<User> users = new ArrayList<>() ;

            int[] result = userService.batchInsert(users, number) ;
            if (result.length == 0 ){
                  return "批量插入失败" ;
            }
            return "批量插入成功" ;
      }

      @RequestMapping("/batchDelete")
      @ResponseBody
      public String batchDelete(int startId, int endId) {
            List idList = new ArrayList() ;
            int result = userService.batchDelete(idList, startId, endId) ;
            if (result == 0 ) {
                  return "批量删除失败" ;
            }
            return "批量删除成功" ;
      }

}
